package org.example.controller;

/**
 *
 */


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import org.example.dao.UserDao;
import org.example.domain.UserDO;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.function.Consumer;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getAll")
    public String getAll() {
        List<UserDO> list = userService.list();
        System.out.println("list:" + list);
        return list.toString();
    }

    @RequestMapping("/insert")
    public String insert() {
        UserDO user = new UserDO();
        user.setUsername("testuser33");
        user.setPassword("12344");
        boolean save = userService.save(user);
        return "sucess";
    }

    @RequestMapping("/update")
    public String update() {
        UserDO one = userService.query().eq("username", "testuser22").one();
        one.setEmail("邮箱");
        userService.updateUser(one);
        return "sucess";
    }

    @RequestMapping("/delete")
    public String delete() {
        UserDO one = userService.query().eq("username", "testuser33").one();
        userService.deleteUser(one);
        return "sucess";
    }

    @RequestMapping("/test")
    public String test() {
        LambdaQueryWrapper<UserDO> query = Wrappers.lambdaQuery();
        query.eq(UserDO::getStatus,1);
        query.or(t->t.eq(UserDO::getUsername,"yuli").isNotNull(UserDO::getUserId));
        query.and(t->t.likeRight(UserDO::getOrgCode,"00109"));
        query.inSql(UserDO::getOrgCode,"select eq_dept from eq_equip_rq");

//        query.eq("username","yuli");
//        query.likeRight("org_code","00107");
//        query.orderByDesc("user_id");
//        query.isNull("pic_id");
//  //      query.lambda().and( wrapper -> wrapper.eq(UserDO::getStatus, 2).or().eq(UserDO::getUserId, 3251544304L));
//        query.ge("gmt_create","2018-01-01");
//        query.eq("status",1);
        List<UserDO> pagelist = userService.pagelist(0, 20,query);
    //    List<UserDO> one = userService.query().select("username", "password").orderByAsc("username").list();

        return "sucess";
    }

    /**
     * 乐观锁测试
     * 当一条数据被修改后 若无重新读取该条数据 不允许直接修改
     * @return
     */
    @RequestMapping("/test2")
    public String test2() {
        UserDO user1 = userService.query().eq("username", "testuser33").one();
        UserDO user2 = userService.query().eq("username", "testuser33").one();

        user1.setEmail("user1@qq.com");
        boolean b1 = userService.updateById(user1);
        if (!b1){
            System.out.println("user1更新失败");
        }
        user2.setEmail("user2@qq.com");
        boolean b2 = userService.updateById(user2);
        if (!b2){
            System.out.println("user2更新失败");
        }
        //结果
        UserDO userDO= userService.query().eq("username", "testuser33").one();
        return "版本："+userDO.getVersion()+"邮箱："+userDO.getEmail() ;
    }

    /**
     * 乐观锁测试
     * @return
     */
    @RequestMapping("/test3")
    public String test3() {
        UserDO user1 = userService.query().eq("username", "testuser33").one();


        user1.setEmail("user1@qq.com");
        boolean b1 = userService.updateById(user1);
        if (!b1){
            System.out.println("user1更新失败");
        }
        UserDO user2 = userService.query().eq("username", "testuser33").one();
        user2.setEmail("user2@qq.com");
        boolean b2 = userService.updateById(user2);
        if (!b2){
            System.out.println("user2更新失败");
        }
        //结果
        UserDO userDO= userService.query().eq("username", "testuser33").one();
        return "版本："+userDO.getVersion()+"邮箱："+userDO.getEmail() ;
    }
}
